package com.xudow.app.dynamicstate_old.module.recommend;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.list.BeamListFragment;
import com.jude.beam.expansion.list.ListConfig;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.utils.JUtils;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.AdvInfo;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.DynamicContentViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.PostLongViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.PostNormalViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.RepostViewHolder;
import com.xudow.app.ui.ActivityViewActivity;
import com.xudow.app.ui.CourseDetailActivity;
import com.xudow.app.ui.CourseListActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by troyl on 2016/4/29.
 */
@RequiresPresenter(RecommendPresenter.class)
public class RecommendFragment extends BeamListFragment<RecommendPresenter, Dynamic> implements View.OnClickListener {
    private static final int POST_NORMAL = 1;
    private static final int POST_LONG = 2;
    private static final int REPOST = 3;

    @BindView(R.id.dynamic_recommend_course)
    LinearLayout mCourse;
    @BindView(R.id.dynamic_recommend_talent)
    LinearLayout mTalent;
    @BindView(R.id.banner)
    RollPagerView banner;

    @Override
    public ListConfig getConfig() {
        return super.getConfig()
                .setContainerEmptyAble(false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        DynamicContentViewHolder viewHolder = null;
        if (viewType == POST_LONG) {
            viewHolder = new PostLongViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == POST_NORMAL) {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == REPOST) {
            viewHolder = new RepostViewHolder(getPresenter().getAdapter(), parent);
        }
        return viewHolder;
    }

    @Override
    public int getViewType(int position) {
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        // 1：长文，2：普通
        int type = dynamic.getType();
        // 上级动态主键, 为0说明，这条动态不是转发的
        long pId = dynamic.getpId();
        if (pId != 0)
            return REPOST;
        else if (type == 1)
            return POST_LONG;
        else if (type == 2)
            return POST_NORMAL;
        return 0;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().addItemDecoration(new DividerDecoration(getResources().getColor(R.color.gray100), JUtils.dip2px(12)));
        getPresenter().getAdapter().addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.dynamic_recommend, parent, false);
                ButterKnife.bind(RecommendFragment.this, view);
                ViewGroup.LayoutParams lp = banner.getLayoutParams();
                lp.height = (int) (JUtils.getScreenWidth() * 0.4);
                banner.setLayoutParams(lp);
                mCourse.setOnClickListener(RecommendFragment.this);
                mTalent.setOnClickListener(RecommendFragment.this);
                initBanner();
                return view;
            }

            @Override
            public void onBindView(View headerView) {

            }
        });
    }

    private void initBanner() {
        DynamicModel.getInstance().getBanner(5)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .subscribe(advInfos -> {
                    if (advInfos != null && advInfos.size() > 0) {
                        banner.setAdapter(new StaticPagerAdapter() {
                            @Override
                            public int getCount() {
                                return advInfos.size();
                            }

                            @Override
                            public View getView(ViewGroup container, int position) {
                                ImageView view = new ImageView(container.getContext());
                                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                                Glide.with(getActivity()).load(ImageModel.getXueDouImage(advInfos.get(position).getImgPath())).into(view);
                                view.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        AdvInfo advInfo = advInfos.get(position);
                                        Intent intent = null;
                                        switch (advInfo.getType()) {
                                            case 1:// 活动
                                                Long activityId = advInfo.getRefId();
                                                if (null != activityId) {
                                                    intent = new Intent(getContext(), ActivityViewActivity.class);
                                                    intent.putExtra("activityId", activityId);
                                                }
                                                break;
                                            case 2://课程
                                                Long courseId = advInfo.getRefId();
                                                if (null != courseId) {
                                                    intent = new Intent(getContext(), CourseDetailActivity.class);
                                                    intent.putExtra("courseId", courseId);
                                                }
                                                break;
                                            case 3://外部链接
                                                intent = new Intent(getContext(), ActivityViewActivity.class);
                                                intent.putExtra("pageurl", advInfo.getPageUrl());
                                                break;
                                            case 4://自编辑页面
                                                intent = new Intent(getContext(), ActivityViewActivity.class);
                                                intent.putExtra("pagecontent", advInfo.getPageContent());
                                                break;

                                        }
                                        if (null != intent)
                                            getActivity().startActivity(intent);

                                    }
                                });
                                return view;
                            }
                        });
                    } else {
                        banner.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dynamic_recommend_course:
                Intent courseIntent = new Intent(getActivity(), CourseListActivity.class);
                courseIntent.putExtra("mode", "recommend");
                startActivity(courseIntent);
                break;
            case R.id.dynamic_recommend_talent:
                Intent talentIntent = new Intent(getActivity(), PreferredTalentActivity.class);
                startActivity(talentIntent);
                break;
        }
    }

}
